package problem.nc078;


import java.util.Stack;

public class Solution {

    static class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }


    public static ListNode ReverseList(ListNode head) {
        if (head == null) {
            return head;
        }
        ListNode cur = head;
        ListNode pre = null;
        ListNode next = cur.next;
        while (next != null) {
            cur.next = pre;
            pre = cur;
            cur = next;
            next = cur.next;
        }
        cur.next = pre;
        return cur;
    }

    public static void main(String[] args) {
        ListNode root = new ListNode(5);
        ListNode a = new ListNode(4);
        ListNode b = new ListNode(3);
        ListNode c = new ListNode(2);
        ListNode d = new ListNode(1);

        root.next = a;
        a.next = b;
        b.next = c;
        c.next =d;

        ListNode x = ReverseList(root);
        while(x != null){
            System.out.println(x.val);
            x = x.next;
        }
    }
}
